<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	ADMINGROUP2 class.
*
*	Administration of institutions
*****/
class ADMINGROUP2
{
// Constructor
	function ADMINGROUP2($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/group/GROUP2.php");
		$this->group = new GROUP2($this->db);
		$this->groups = $this->group->grabAll();
// Administering 'General' category (key == 1) is not allowed.
		unset($this->groups[1]);
	}
// check we really are admin
	function gateKeep($method)
	{
		if(!$this->session->getVar("setup_superadmin"))
		{
			include_once("core/authorize/AUTHORIZE.php");
			$authorize = new AUTHORIZE($this->db);
			if($pString = $authorize->initLogon())
				return $pString;
		}
// else, run $method
		return $this->$method();
	}
// display options for categories
	function display($message = FALSE)
	{
		$this->template->setVar("heading", $this->messages->text("heading", "institution"));
		$pString = '';
		if($message)
			$pString .= MISC::p($message);
		$pString .= MISC::p(MISC::a("link", $this->messages->text("institution", "addLabel"), 
			"index.php?action=adminGroupAddInit2"));
		if(sizeof($this->groups))
		{
			$pString .= MISC::p(MISC::a("link", $this->messages->text("institution", "deleteLabel"), 
				"index.php?action=adminGroupDeleteInit2"));
			$pString .= MISC::p(MISC::a("link", $this->messages->text("institution", "editLabel"), 
				"index.php?action=adminGroupEditDisplay2"));
		}
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Add a group - display options.
	function addInit($error = FALSE)
	{
		$this->template->setVar("heading", $this->messages->text("heading", "institution", 
		" (" . $this->messages->text("institution", "addLabel") . ")"));
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= FORM::formHeader("adminGroupAdd2");
		$pString .= TABLE::tableStart(FALSE, 0, 0, 0, "left");
		$pString .= TABLE::trStart();
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "acronym"), "add_acronym", FALSE, 30, 255));
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "name"), "add_name", FALSE, 30, 255));
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "address"), "add_address", FALSE, 30, 255));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::trStart();
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "website"), "add_website", FALSE, 30, 255));
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "country"), "add_country", FALSE, 30, 255));
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "comment"), "add_comment", FALSE, 30, 255));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= FORM::formSubmit('Add');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Add a group
	function add()
	{
		if(!$input = $this->validateInput('add'))
			$this->badInput($this->errors->text("inputError", "invalid"), 'addInit');
		if(!$this->group->addSql($input))
			$this->badInput($this->errors->text("inputError", "invalid"));
		$pString = $this->success->text("institution", " " . $this->messages->text("misc", "added") . " ");
		$this->groups = $this->group->grabAll();
		unset($this->groups[1]);
		return $this->display($pString);
	}
// Delete groups display.
	function deleteInit($error = FALSE)
	{
		$this->template->setVar("heading", $this->messages->text("heading", "institution", 
		" (" . $this->messages->text("institution", "deleteLabel") . ")"));
		$pString = '';
		$this->groups = $this->group->grabAll();
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= MISC::p($this->messages->text("institution", "deleteWarning"));
		$pString .= FORM::formHeader("adminGroupDeleteConfirm2");
		foreach($this->groups as $key => $value)
			$pString .= FORM::checkbox(FALSE, "groupDelete_" . $key) . " $value" . MISC::br();
		$pString .= MISC::br() . FORM::formSubmit('Delete');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
	
// Ask for confirmation of delete groups
	function deleteConfirm()
	{
		if(!$input = $this->validateInput('delete'))
			$this->badInput($this->errors->text("inputError", "invalid"), 'deleteInit');
		$this->template->setVar("heading", $this->messages->text("heading", "institution", 
			" (" . $this->messages->text("institution", "deleteLabel") . ")"));
		$groups = "'" . implode("', '", array_keys(array_intersect(array_flip($this->groups), $input))) . "'";
		$groups = html_entity_decode($groups);
		$pString = MISC::p($this->messages->text("institution", "deleteConfirm", ": $groups"));
		$pString .= FORM::formHeader("adminGroupDelete2");
		foreach($input as $id)
			$pString .= FORM::hidden("groupDelete_" . $id, $id);
		$pString .= MISC::br() . FORM::formSubmit('Confirm');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Delete group(s)
	function delete()
	{
		if(!$input = $this->validateInput('delete'))
			$this->badInput($this->errors->text("inputError", "invalid"), 'deleteInit');
		if(!$this->group->deleteSql($input))
			$this->badInput($this->errors->text("inputError", "invalid"), 'deleteInit');
		$groups = "'" . implode("', '", array_keys(array_intersect(array_flip($this->groups), $input))) . "'";
		$pString = $this->success->text("institution", " " . $this->messages->text("misc", "deleted") . " ");
		$this->groups = $this->group->grabAll();
		unset($this->groups[1]);
		return $this->display($pString);
	}
// display groups for editing
	function displayEdit($message = FALSE)
	{
		
		/*$this->template->setVar("heading", $this->messages->text("heading", "institution", 
		" (" . $this->messages->text("institution", "editLabel") . ")"));
		$pString = '';
		if($error)
			$pString .= MISC::p($error, "error", "center");
		$pString .= FORM::formHeader("adminGroupEdit2");
		foreach($this->groups as $key => $value)
			$pString .= FORM::textInput(FALSE, "groupEdit_" . $key, $value, 30, 255) . MISC::br();
		$pString .= MISC::br() . FORM::formSubmit('Edit');
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();*/
		
		$this->template->setVar('heading', $this->messages->text("heading", "edit", " (" . 
			$this->messages->text("institution", "editLabel") . ")"));
		$pString = $message ? $message : FALSE;
		$institutions = $this->group->grabAll();
		if(!$institutions)
		{
			$this->template->setVar('body', $this->messages->text("misc", "noInstitution"));
			return $this->template->process();
		}
		$pString .= FORM::formHeader("adminGroupEditInit2");
		$pString .= FORM::selectFBoxValue(FALSE, "edit_institutionId", $institutions, 20);
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();	
	}

// display the editing form:
	function editInit()
	{
		print_r("test");
		$this->template->setVar('heading', $this->messages->text("heading", "edit", " (" . 
			$this->messages->text("institution", "editLabel") . ")"));
		$institutions = $this->group->grabAll();
		$sessionVars = $this->fromDbToSession($this->vars['edit_institutionId']);
		$pString = FORM::formHeader("adminGroupEdit2");
		$pString .= FORM::hidden("edit_institutionId", $this->vars['edit_institutionId']);
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= TABLE::tableStart(FALSE, 0, 0, 0, "left");
		$pString .= TABLE::trStart();
		$text = array_key_exists("acronym", $sessionVars) ? $sessionVars["acronym"] : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "acronym"), "add_acronym", $text, 30, 255));
		$text = array_key_exists("name", $sessionVars) ? $sessionVars["name"] : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "name"), "add_name", $text, 30, 255));
		$text = array_key_exists("address", $sessionVars) ? $sessionVars["address"] : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "address"), "add_address", $text, 30, 255));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::trStart();
		$text = array_key_exists("website", $sessionVars) ? $sessionVars["website"] : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "website"), "add_website", $text, 30, 255));
		$text = array_key_exists("country", $sessionVars) ? $sessionVars["country"] : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "country"), "add_country", $text, 30, 255));
		$text = array_key_exists("comment", $sessionVars) ? $sessionVars["comment"] : FALSE;
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "comment"), "add_comment", $text, 30, 255));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::br() . "&nbsp;" . MISC::br();
		$pString .= MISC::p(FORM::formSubmit("Proceed"), FALSE, "right");
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	
	}
// Edit groups
	function edit()
	{
		if(!$input = $this->validateInput('edit'))
			$this->badInput($this->errors->text("inputError", "invalid"), 'editInit');
		if(!$this->group->updateSql($input))
			$this->badInput($this->errors->text("inputError", "invalid"));
		$pString = $this->success->text("institution", " " . $this->messages->text("misc", "edited") . " ");
		return $this->display($pString);
	}
	
// bad Input function
	function badInput($error, $method)
	{
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->$method($error));
	}
// validate input
	function validateInput($type)
	{
		if($type == 'add')
		{
			$input = array();
			if(!$input[1] = trim($this->vars['add_acronym']))
				return FALSE;
			
			if(!$input[2] = trim($this->vars['add_name']))
				return FALSE;
			else
			{
				$input[3] = trim($this->vars['add_address']);
				$input[4] = trim($this->vars['add_website']);
				$input[5] = trim($this->vars['add_country']);
				$input[6] = trim($this->vars['add_comment']);			
			}
		}
		else if($type == 'delete')
		{
			foreach($this->vars as $key => $value)
			{
				if(!preg_match("/groupDelete_(.*)/", $key, $match))
					continue;
				$input[] = $match[1];
			}
			if(!isset($input))
				return FALSE;
		}
		else if($type == 'edit')
		{
			$input=array();
			$input[0] = trim($this->vars['edit_institutionId']);
			$input[1] = trim($this->vars['add_acronym']);
			$input[2] = trim($this->vars['add_name']);
			$input[3] = trim($this->vars['add_address']);
			$input[4] = trim($this->vars['add_website']);
			$input[5] = trim($this->vars['add_country']);
			$input[6] = trim($this->vars['add_comment']);	
			if(!isset($input))
				return FALSE;
		}
		return $input;
	}
	
// from database to session
	function fromDbToSession($id)
	{
		
		$recordset = $this->db->select(array('WKX_institution'), 
			array("acronym", "name", "address", "website","country","comment"), 
			" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($id));
		$row = $this->db->fetchRow($recordset);
		// if($row['acronym'])
			// $this->session->setVar("edit_acronym", $this->html->dbToFormTidy($row['acronym']));
		// if($row['name'])	
			// $this->session->setVar("edit_name", $this->html->dbToFormTidy($row['name']));
		return $row;
	}
	
	
}
?>
